package com.example.vaccine.controller;

import com.example.vaccine.VO.PageVO;
import com.example.vaccine.VO.RecordVO;
import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.form.CreateVaccineForm;
import com.example.vaccine.service.VaccinesService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@RestController
@RequestMapping("/vaccines")
public class VaccinesController {

    @Autowired
    private VaccinesService vaccinesService;

    @Operation(summary = "创建疫苗记录", description = "根据请求体创建疫苗数据")
    @PostMapping("/create")
    public ResultVO create(@Parameter(description = "疫苗创建表单", required = true) @RequestBody CreateVaccineForm createForm){
        Boolean create = this.vaccinesService.create(createForm);
        if(!create)
            return ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }

    /**
     * 根据疫苗编号查询单条疫苗信息
     */
    @Operation(summary = "根据疫苗编号查询疫苗信息", description = "查询指定疫苗编号的疫苗详情列表")
    @GetMapping("/singleList/{numbering}")
    public ResultVO singleList(
            @Parameter(description = "疫苗编号", required = true) @PathVariable("numbering") String numbering) {
        List<RecordVO> recordVOList = vaccinesService.singleVaccine(numbering);
        return ResultVOUtil.success(recordVOList);
    }

    /**
     * 分页查询疫苗列表
     */
    @Operation(summary = "分页查询疫苗列表", description = "获取疫苗分页列表，传入页码和每页大小")
    @GetMapping("/vaccineList/{page}/{size}")
    public ResultVO list(
            @Parameter(description = "页码，从1开始", required = true) @PathVariable("page") Integer page,
            @Parameter(description = "每页条数", required = true) @PathVariable("size") Integer size) {
        PageVO resultPage = vaccinesService.vaccineList(page, size);
        return ResultVOUtil.success(resultPage);
    }

    /**
     * 根据ID删除疫苗，删除后返回当前分页列表
     */
    @Operation(summary = "删除疫苗记录", description = "根据ID删除疫苗，并返回删除后分页列表")
    @GetMapping("/delete/{id}/{page}/{size}")
    public ResultVO delete(
            @Parameter(description = "疫苗ID", required = true) @PathVariable("id") Integer id,
            @Parameter(description = "当前页码", required = true) @PathVariable("page") Integer page,
            @Parameter(description = "每页大小", required = true) @PathVariable("size") Integer size) {
        Boolean result = vaccinesService.delte(id);
        if (!result) {
            return ResultVOUtil.fail();
        }
        PageVO resultPage = vaccinesService.vaccineList(page, size);
        return ResultVOUtil.success(resultPage);
    }


    /**
     * 更新疫苗信息
     */
    @Operation(summary = "更新疫苗记录", description = "根据请求体更新疫苗信息")
    @PostMapping("/update")
    public ResultVO update(@Parameter(description = "疫苗更新表单", required = true) @RequestBody CreateVaccineForm createForm) {
        Boolean update = vaccinesService.update(createForm);
        if (!update) {
            return ResultVOUtil.fail();
        }
        return ResultVOUtil.success(null);
    }
}
